package CelestialDataAccess;

import javax.sql.rowset.*; //provides cached row set interface
import java.sql.*;         //provides connection object and sql exception
import java.util.Date;
import CelestialDataStructures.*;
import java.util.Calendar;

public class Employee implements IPersistable, Comparable{

    private String m_strUserName;
    private String m_strPassword;
    private int m_iUserLevel;
    private int m_iEmployeeID;
    private Date m_dtEmployedOn;
    
    private String m_strFirstName;
    private String m_strLastName;
    private String m_strContact;
    private String m_strEmail;
    private Date m_dtDOB;

    private String m_strAddressLine1;
    private String m_strAddressLine2;
    private String m_strPostalCode;

    private boolean m_bIsActive;

    private static EmployeesDataAccess daEmployees = new EmployeesDataAccess();

    /*************************************************************************/
    /* get and set methods */
    public String getUsername(){return m_strUserName;}
    public void setUsername(String uname){ m_strUserName = uname; }
    /*-----------------------------------------------------------------------*/
    public String getPassword(){return m_strPassword;}
    public void setPassword(String pw){ m_strPassword = pw; }
    /*-----------------------------------------------------------------------*/
    public int getUserLevel(){return m_iUserLevel;}
    public void setUserLevel(int lvl){ m_iUserLevel = lvl; }
    /*-----------------------------------------------------------------------*/
    public int getEmployeeID(){ return m_iEmployeeID;}
    /*-----------------------------------------------------------------------*/
    public Date getEmploymentDate(){ return m_dtEmployedOn;}
    public void setEmploymentDate(Date emp_date){ m_dtEmployedOn = emp_date; }
    /*-----------------------------------------------------------------------*/
    public String getName(){ return m_strFirstName + " " + m_strLastName;}
    /*-----------------------------------------------------------------------*/
    public String getFirstName(){return m_strFirstName;}
    public void setFirstName(String fname){ m_strFirstName = fname; }
    /*-----------------------------------------------------------------------*/
    public String getLastName(){return m_strLastName;}
    public void setLastName(String lname){ m_strLastName = lname; }
    /*-----------------------------------------------------------------------*/
    public String getContact(){return m_strContact;}
    public void setContact(String contact){ m_strContact = contact; }
    /*-----------------------------------------------------------------------*/
    public String getEmail(){return m_strEmail;}
    public void setEmail(String email){ m_strEmail = email; }
    /*-----------------------------------------------------------------------*/
    public int getAge(){return 0;}
    public Date getDOB(){return m_dtDOB;}
    public void setDOB(Date dob){ m_dtDOB = dob; }
    /*-----------------------------------------------------------------------*/

    public String getAddressLine1(){return m_strAddressLine1;}
    public void setAddressLine1(String addr1){ m_strAddressLine1 = addr1; }
    /*-----------------------------------------------------------------------*/
    public String getAddressLine2(){return m_strAddressLine2;}
    public void setAddressLine2(String addr2){ m_strAddressLine2 = addr2; }
    /*-----------------------------------------------------------------------*/
    public String getPostalCode(){return m_strPostalCode;}
    public void setPostalCode(String pcode){ m_strPostalCode = pcode; }
    /*-----------------------------------------------------------------------*/

    public boolean isActive(){return m_bIsActive;}
    public void setActive(Boolean active){ m_bIsActive = active; }

    /*************************************************************************/
    public @Override String toString(){ return getName(); }
    
    /*************************************************************************/
    /* Constructors */
    public Employee(){
        m_iEmployeeID = 0;
        m_dtEmployedOn = new Date();
    }
    /*-----------------------------------------------------------------------*/
    static Employee createEmployee(CachedRowSet crs) throws Exception{

        Employee emp;

        try{
             if(!crs.last())
                return null;

            emp = new Employee();
            emp.m_strUserName = crs.getString("Username");
            emp.m_strPassword = CryptService.decrypt(crs.getBytes("Password"));
            emp.m_iUserLevel =crs.getInt("UserLevel");
            emp.m_iEmployeeID = crs.getInt("EmployeeID");
            emp.m_dtEmployedOn = crs.getDate("EmployedOn");

            emp.m_strFirstName = crs.getString("FirstName");
            emp.m_strLastName = crs.getString("LastName");
            emp.m_strContact = crs.getString("Contact");
            emp.m_strEmail = crs.getString("Email");
            emp.m_dtDOB = crs.getDate("DOB");
            emp.m_strAddressLine1 = crs.getString("AddressLine1");
            emp.m_strAddressLine2 = crs.getString("AddressLine2");
            emp.m_strPostalCode = crs.getString("PostalCode");

            emp.m_bIsActive  =crs.getBoolean("IsActive");
        }catch(Exception ex){
            return null;
        }
        return emp;
    }
    /*************************************************************************/
    /* IPersistable */
    public int insert(){
        return daEmployees.insertEmployee(this);
    }
    /*-----------------------------------------------------------------------*/
    public int delete(){
        return daEmployees.deleteEmployee(this);
    }
    /*-----------------------------------------------------------------------*/
    public int update(){
        return daEmployees.updateEmployee(this);
    }
    /*************************************************************************/
    /* IComparable */
    public int compareTo(Object cust){

        if(this.m_iEmployeeID==((Employee)cust).m_iEmployeeID)
            return 0;
        else if(this.m_iEmployeeID > ((Employee)cust).m_iEmployeeID)
            return 1;
        else
            return -1;
    }
}
